var errorContainer;
var controlContainer;
function ManagementProduct(errorcontainer,controlcontainer){
    errorContainer=errorcontainer;
    controlContainer=controlcontainer;
}
ManagementProduct.prototype.toggleActivation=function(proid){
    var xmlhttp;
    var data="action=PRODUCT_ACTIVATION&proid="+proid;
    xmlhttp=getXmlHttpObj();
    xmlhttp.open("GET",contextPath+"/ProductController?"+data,true);
    xmlhttp.send();
    xmlhttp.onreadystatechange=function(e){
        var current=e.target;
        if (current.readyState==4 && current.status==200)
        {
            var errorXml=xmlhttp.responseXML;
            errorContainer.innerHTML="";
    
            if(errorXml){
                errorContainer.innerHTML=xmlhttp.responseXML.getElementsByTagName("error")[0].textContent.fontcolor("red");
            }else{
                var resString=xmlhttp.responseText;
                controlContainer.innerHTML=resString;
            }
        }
    }
}
ManagementProduct.prototype.viewComments=function(proid, proname){
    var btnClose = document.getElementById("btnClose");
    var popupContainer =  document.getElementById("PopupContainer");
    var content = document.getElementById("content");
    var overlay = document.getElementById("fadeOverlay");
    var catString = loadXmlFilteredCommentsOnProduct(proid, proname);
    var popupCategory = new Lightbox(btnClose, overlay, popupContainer, content)

    content.innerHTML = catString;
    popupCategory.show();
    popupCategory.onContainerClick=function(e){
        var currentObj=e.target;
        var errorPopupContainer = document.getElementById("error-container");
        var controlPopupContainer = document.getElementById("CommentContainer");
        var managementComment = new ManagementComment();
        managementComment.initController(errorPopupContainer,controlPopupContainer);
        var link = currentObj.className.split(' ')[2];
        if(link=="linkDelete"){
            var comid=currentObj.attributes["comid"].value;
            managementComment.deleteComment(comid,proid);
        }
    }
}

ManagementProduct.prototype.viewAdd=function(){
    var btnClose = document.getElementById("btnClose");
    var popupContainer =  document.getElementById("PopupContainer");
    var content = document.getElementById("content");
    var overlay = document.getElementById("fadeOverlay");
    var catString = loadXmlFilteredSaveOnProduct();
    var popupCategory = new Lightbox(btnClose, overlay, popupContainer, content)

    content.innerHTML = catString;
    popupCategory.show();
    popupCategory.onContainerClick=function(e){
        var currentObj=e.target;
        if(currentObj.id=="saveProduct"){
            var txtname=document.getElementById("txtName");
            var txtimgurl=document.getElementById("txtImageUrl");
            var txtPrice=document.getElementById("txtPrice");
            var txtdescription=document.getElementById("txtDescription");
            
            if(txtname&&txtimgurl&&txtPrice&&txtdescription){
                var xmlhttp;
                var data="action=PRODUCT_ADD&name="+txtname.value
                +"&imageurl="+txtimgurl.value
                +"&price="+txtPrice.value
                +"&description="+txtdescription.value
                xmlhttp=getXmlHttpObj();
                xmlhttp.open("GET",contextPath+"/ProductController?"+data,true);
                xmlhttp.send();
                xmlhttp.onreadystatechange=function(e){
                    var errorName=document.getElementById("error-Name");
                    var errorPrice=document.getElementById("error-Price");
                    //var controlcontainer=document.getElementById("NewProductContainer");
                    var current=e.target;
                    if (current.readyState==4 && current.status==200)
                    {
                        var errorXml=xmlhttp.responseXML;
                        errorName.innerHTML="";
                        errorPrice.innerHTML="";
                        if (errorXml){
                            if(errorXml.firstChild.localName=="errorName"){
                                errorName.innerHTML=xmlhttp.responseXML.getElementsByTagName("errorName")[0].textContent.fontcolor("red");
                            }else if (errorXml.firstChild.localName=="errorPrice"){
                                errorPrice.innerHTML=xmlhttp.responseXML.getElementsByTagName("errorPrice")[0].textContent.fontcolor("red");
                            }
                        } else {
                            popupCategory.hide();
                            var resString=xmlhttp.responseText;
                            controlContainer.innerHTML=resString;
                        }
                    }
                }
            }
        }
    }
}

function saveNewProductAdmin(){

}

function saveNewRestaurantCategory(cateids,defaultCateIds,resid){
    var xmlhttp;
    var data="action=RESTAURANT_CATEGORY_ADD&cateids=" + cateids + "&defaultCateIds=" +defaultCateIds + "&resid=" +resid;
    xmlhttp=getXmlHttpObj();
    xmlhttp.open("GET",contextPath+"/ProductController?"+data,false);
    xmlhttp.send();
//    var errorXml=xmlhttp.responseXML;
//    var errorContainer=document.getElementById("ErrorContainer");
//    errorContainer.innerHTML="";
}

function saveNewRestaurantLocationFilter(locIds, locDeliveryFee, defaultLocIds, resid){
    var xmlhttp;
    var data="action=RESTAURANT_LOCATION_ADD&locIds=" + locIds +"&locDeliveryFee=" + locDeliveryFee + "&defaultLocIds=" +defaultLocIds + "&resid=" +resid;
    xmlhttp=getXmlHttpObj();
    xmlhttp.open("GET",contextPath+"/ProductController?"+data,false);
    xmlhttp.send();
//    var errorXml=xmlhttp.responseXML;
//    var errorContainer=document.getElementById("ErrorContainer");
//    errorContainer.innerHTML="";
}


function loadXmlFilteredCommentsOnProduct(proid,proname){
    var xmlhttp;
    var data="action=TRANSFORM_FILTER_COMMENTS_BY_PRODUCT&proid=" + proid + "&proname=" + proname;
    xmlhttp=getXmlHttpObj();
    xmlhttp.open("GET", contextPath+"/FlowController?"+data, false);
    xmlhttp.send();
    return xmlhttp.responseText;
}

function loadXmlFilteredSaveOnProduct(){
    var xmlhttp;
    var data="action=TRANSFORM_FILTER_SAVE_BY_PRODUCT"
    xmlhttp=getXmlHttpObj();
    xmlhttp.open("GET", contextPath+"/FlowController?"+data, false);
    xmlhttp.send();
    return xmlhttp.responseText;
}
ManagementProduct.prototype.updateProduct=function(current,proid,name,img,des,price){
    var xmlhttp;
    var data="action=PRODUCT_UPDATE&proid="+proid+"&name="+
    name+"&des="+des+"&price="+price;
    xmlhttp=getXmlHttpObj();
    xmlhttp.open("GET",contextPath+"/ProductController?"+data,true);
    xmlhttp.send();
    xmlhttp.onreadystatechange=function(e){
        var current=e.target;
        if (current.readyState==4 && current.status==200)
        {
            var errorXml=xmlhttp.responseXML;
            errorContainer.innerHTML="";
    
            if(errorXml){
                errorContainer.innerHTML=xmlhttp.responseXML.getElementsByTagName("error")[0].textContent.fontcolor("red");
            }else{
                var resString=xmlhttp.responseText;
                current.innerHTML="Update";
                controlContainer.innerHTML=resString;
            }
        }
    }
}
ManagementProduct.prototype.updateRestaurantDetails=function(restaurant){
    var xmlhttp;
    var data="action=RESTAURANT_DETAIL_UPDATE&resid="+restaurant.resid
    +"&name="+restaurant.name
    +"&imageurl="+restaurant.url
    +"&minimumOrder="+restaurant.minimumOrder
    +"&optime="+restaurant.optime
    +"&address="+restaurant.address
    +"&description="+restaurant.description
    +"&introduction="+restaurant.introduction
    +"&phone="+restaurant.phone;
    xmlhttp=getXmlHttpObj();
    xmlhttp.open("GET",contextPath+"/ProductController?"+data,true);
    xmlhttp.send();
    xmlhttp.onreadystatechange=function(e){
        var current=e.target;
        if (current.readyState==4 && current.status==200)
        {
            var errorXml=xmlhttp.responseXML;
            errorContainer.innerHTML="";

            if(errorXml){
                errorContainer.innerHTML=xmlhttp.responseXML.getElementsByTagName("error")[0].textContent.fontcolor("red");
            }else{
                var parent = document.getElementById("RestaurantDetailContainer");
                var resString=xmlhttp.responseText;
                parent.innerHTML=resString;
            }
        }
    }
}
ManagementProduct.prototype.saveNewRestaurant=function(restaurant){
    var xmlhttp;
    var data="action=RESTAURANT_ADD&name="+restaurant.name
    +"&imageurl="+restaurant.url
    +"&minimumOrder="+restaurant.minimumOrder
    +"&optime="+restaurant.optime
    +"&address="+restaurant.address
    +"&description="+restaurant.description
    +"&introduction="+restaurant.introduction
    +"&phone="+restaurant.phone;
    xmlhttp=getXmlHttpObj();
    xmlhttp.open("GET",contextPath+"/ProductController?"+data,true);
    xmlhttp.send();
    xmlhttp.onreadystatechange=function(e){
        var current=e.target;
        if (current.readyState==4 && current.status==200)
        {
            var errorXml=xmlhttp.responseXML;
            errorContainer.innerHTML="";
    
            if(errorXml){
                errorContainer.innerHTML=xmlhttp.responseXML.getElementsByTagName("error")[0].textContent.fontcolor("red");
            }else{
                var resString=xmlhttp.responseText;
                controlContainer.innerHTML=resString;
            }
        }
    }
}
